package com.fanix5.javamvvm.base;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;

public abstract class LazyBaseFragment<DB extends ViewDataBinding> extends Fragment {

    private View rootView; //跟布局
    protected DB mDatabind;
    private boolean isViewCreated = false;//跟布局是否已经创建
    private boolean currentVisibleState = false;//当前View可见状态
    private String TAG = this.getClass().getSimpleName();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        if(rootView == null){
            mDatabind = DataBindingUtil.inflate(inflater,getLayoutId(),container,false);
            rootView = mDatabind.getRoot();
        }

        isViewCreated = true;
        if(getUserVisibleHint()){
            dispatchUserVisibleHint(true);
        }
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initListener();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    //判断fragmen是否可见
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        Log.e(TAG, "setUserVisibleHint: "+isVisibleToUser );

        if(isViewCreated){
            if(isVisibleToUser && !currentVisibleState){
                dispatchUserVisibleHint(true);
            }else if(!isVisibleToUser && currentVisibleState) {
                dispatchUserVisibleHint(false);
            }
        }

    }




    //分发fragment可见时间
    private void dispatchUserVisibleHint(boolean isVisible){
        Log.e(TAG, "dispatchUserVisibleHint: "+isVisible );

        if(currentVisibleState == isVisible){
            return;
        }

        currentVisibleState = isVisible;

        if(isVisible){
            onFragmentLoad();
        }else {
            onFragmentLoadStop();
        }

    }

    /**
     * 懒加载开始
     */
    protected void onFragmentLoad(){
        Log.e(TAG, "onFragmentLoad: "+"真正更新数据");
    }

    /**
     * 懒加载结束
     */
    private void onFragmentLoadStop(){
        Log.e(TAG, "onFragmentLoadStop: "+"停止一些更新" );
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e(TAG, "onResume: " );
        if(!currentVisibleState && getUserVisibleHint()){
            dispatchUserVisibleHint(true);
        }

    }

    @Override
    public void onPause() {
        super.onPause();
        Log.e(TAG, "onPause: " );
        if(currentVisibleState && getUserVisibleHint()){
            dispatchUserVisibleHint(false);
        }


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.e(TAG, "onDestroyView: " );

        isViewCreated = false;
        currentVisibleState = false;

        if(mDatabind != null){
            mDatabind = null;
        }
        if (rootView.getParent() != null) {
            ((ViewGroup) rootView.getParent()).removeView(rootView);
        }

        if(rootView != null){
            rootView = null;
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        Log.e(TAG, "onDestroy: " );
    }

    /**
     * 初始化View的代码写在这个方法中
     */
    protected abstract void initView(

    );

    /**
     * 初始化监听器的代码写在这个方法中
     */
    protected abstract void initListener();

    /**
     * 初始数据的代码写在这个方法中，用于从服务器获取数据
     */
    protected abstract void initData();


    /**
     * 获取布局 Id
     */
    protected abstract @LayoutRes
    int getLayoutId();
}
